﻿using System;
using EzDesk.DesktopClient.AppModule.ViewContract;
using EzDesk.DesktopClient.Event;
using EzDesk.MVVMBase;
using System.Windows;
using EzDesk.Generic.WPF;


namespace EzDesk.DesktopClient.AppModule.View
{
    /// <summary>
    /// Interaction logic for MainPanelView.xaml
    /// </summary>
    public partial class MainPanelView : WindowExtenBase, IMainPanelView
    {
        private bool _isMainWindowDisplayOnScreen = true;

        //TODO: Refactorying kinds of event to property and triggers
        public MainPanelView()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            ShowInTaskbar = false;
            MoreFunctionRegion.SizeChanged += MoreFunctionRegion_SizeChanged;
            MainWindowMoveEvent.Instance.Subscribe(this.OnWindowStateChanged);
        }

        private void OnWindowStateChanged(MainWindowMoveArgs obj)
        {
            _isMainWindowDisplayOnScreen = ((Window)obj.MainWindow).IsDisplayOnScreen();
            ResetView();
        }

        void MoreFunctionRegion_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            AdjustSize();
        }

        protected override void OnMouseLeave(System.Windows.Input.MouseEventArgs e)
        {
            ResetView();
            base.OnMouseLeave(e);
        }

        private void ResetView()
        {
            if (_isMainWindowDisplayOnScreen)
                this.ToBeTop();
            MoreFunctionRegion.Visibility = Visibility.Collapsed;
            ShortcutRegion.Visibility =
                _isMainWindowDisplayOnScreen ? Visibility.Visible : Visibility.Collapsed;

            Bar.Visibility =
                _isMainWindowDisplayOnScreen ? Visibility.Collapsed : Visibility.Visible;
            AdjustSize();

        }

        protected override void OnMouseEnter(System.Windows.Input.MouseEventArgs e)
        {
            if(_isMainWindowDisplayOnScreen) return;

            Bar.Visibility = Visibility.Collapsed;
            ShortcutRegion.Visibility = Visibility.Visible;
            this.Height = 60;
            base.OnMouseEnter(e);
            AdjustSize();
        }

        private void AdjustSize()
        {
            Dispatcher.Invoke(new Action(() =>
                {
                    Height =
                    MoreFunctionRegion.ActualHeight + ShortcutRegion.ActualHeight + 5;
                })
            );
        }

        public void ShowMoreFunctions()
        {
            MoreFunctionRegion.Visibility = Visibility.Visible;
            AdjustSize();
        }
    }
}
